//
//  ViewController.m
//  flutter
//
//  Created by GilesZhou on 2020/6/19.
//  Copyright © 2020 Ganee. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
#import <Flutter/Flutter.h>
#import "CrashManager.h"



#import <Realm.h>
#import "RealmManager.h"
#import "RealmCrashModel.h"


@interface ViewController ()<UIDocumentPickerDelegate>
@property (nonatomic, strong) UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = UIColor.redColor;
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"flutter" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        make.width.height.mas_equalTo(100);
    }];
    
    UIButton *crashbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    crashbutton.backgroundColor = UIColor.redColor;
    [crashbutton setTitle:@"crash" forState:UIControlStateNormal];
    crashbutton.frame = CGRectMake(100, 100, 100, 100);
    [crashbutton addTarget:self action:@selector(crash) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:crashbutton];
    
    [crashbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(button);
        make.left.mas_equalTo(button.mas_right).offset(30);
        make.width.height.mas_equalTo(100);
    }];
    
    _textField = [[UITextField alloc] init];
    _textField.backgroundColor = UIColor.yellowColor;
    [self.view addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(button.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(300, 30));
    }];
}

- (void)buttonAction {
    FlutterViewController *flutterViewController = [FlutterViewController new];
    [flutterViewController setInitialRoute:_textField.text];
    [self.navigationController pushViewController:flutterViewController animated:YES];
    
    FlutterEventChannel *flutterEvent = [FlutterEventChannel eventChannelWithName:@"flutter_iOS" binaryMessenger:flutterViewController.binaryMessenger];
}

- (void)crash {

//    NSString *str = @"asda1";
//    NSLog(@"%@", str);

//    NSString *str = nil;
//    NSArray *arr = @[str];
    
//    NSLog(@"%@", arr[1]);
    
//    NSMutableArray *arrM = [NSMutableArray arrayWithObject:str];
//    NSLog(@"%@", arrM[0]);
    
    
//    NSLog(@"%@", [RealmManager searchDataWithType:RealmSaveTypeCrash]);
    
    
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc]
                                                      initWithDocumentTypes:@[@"com.adobe.pdf"] inMode:UIDocumentPickerModeImport];
    documentPicker.navigationItem.leftBarButtonItem.tintColor = UIColor.blueColor;
    documentPicker.navigationItem.rightBarButtonItem.tintColor = UIColor.redColor;

    if (@available(iOS 11.0, *)) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UIDocumentBrowserViewController class]]] setTintColor:UIColor.blueColor];
    }
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

@end
